package com.gonsin.gnews.mapper;

import com.gonsin.gnews.annotations.model.NewsParamsPoConverter;
import com.gonsin.gnews.annotations.model.NewsTemplateBean;
import com.gonsin.gnews.annotations.model.NewsTemplateSearchPo;
import org.apache.ibatis.annotations.*;
import org.springframework.data.domain.Pageable;

import java.util.List;

/**
 * 消息模板数据库接口
 */
@Mapper
//@Component
public interface NewsTemplateMapper  {


    /**
     * 根据条件搜索模板
     */
    @Results(
            id = "NewsTemplateBean",
            value = {
                    @Result(property = "newsTemplateKey", column = "news_template_key"),
                    @Result(property = "templateKey", column = "template_key"),
                    @Result(property = "params", column = "params", typeHandler = NewsParamsPoConverter.class),
                    @Result(property = "smsContent", column = "sms_content"),
                    @Result(property = "templateCode", column = "sms_template_code"),
                    @Result(property = "createTime", column = "create_time"),
                    @Result(property = "updateTime", column = "update_time"),
                    @Result(property = "delete", column = "gs_delete"),
                    @Result(property = "edificeKey", column = "edifice_key"),
            }
    )
    @Select("<script>" +
            "select m.* from gs_news_template as m " +
            "where m.edifice_key = #{edificeKey} " +
            "<if test=\"searchPo.name != null and searchPo.name != ''\">" +
            "   and m.name like concat('%', #{searchPo.name}, '%') " +
            "</if>" +
            "<if test=\"searchPo.platform != null and searchPo.platform != ''\">" +
            "   and m.platform = #{searchPo.platform} " +
            "</if>" +
            "<if test=\"searchPo.templateKey != null and searchPo.templateKey != ''\">" +
            "   and m.template_key = #{searchPo.templateKey} " +
            "</if>" +
            "<if test=\"searchPo.enable != null\">" +
            "   and m.enable = #{searchPo.enable} " +
            "</if>" +
            "<if test=\"searchPo.newsTemplateKeys != null and searchPo.newsTemplateKeys.size() &gt; 0\">" +
            "   and m.news_template_key in " +
            "   <foreach item=\"item\" collection=\"searchPo.newsTemplateKeys\" open=\"(\" close=\")\" separator=\",\">" +
            "       #{item}" +
            "   </foreach>" +
            "</if>" +
            "<if test=\"searchPo.excludedPlatform != null and searchPo.excludedPlatform.size() &gt; 0\">" +
            "   and m.platform not in " +
            "   <foreach item=\"item\" collection=\"searchPo.excludedPlatform\" open=\"(\" close=\")\" separator=\",\">" +
            "       #{item}" +
            "   </foreach>" +
            "</if>" +
            "<if test=\"!pageable.sort.toString().equals('UNSORTED')\">" +
            "    order by " +
            "    <foreach item=\"item\" index=\"index\" collection=\"pageable.sort.orders\" separator=\",\">" +
            "        m.${item.property} ${item.direction} " +
            "    </foreach>" +
            "</if>" +
            "limit ${pageable.page * pageable.size}, ${pageable.size}" +
            "</script>")
    List<NewsTemplateBean> searchTemplates(
            @Param("edificeKey") String edificeKey, @Param("searchPo") NewsTemplateSearchPo searchPo,
            @Param("pageable") Pageable pageable);



    @ResultMap("NewsTemplateBean")
    @Select("<script>" +
            "select m.* from gs_news_template as m " +
            "where m.news_template_key = #{newsTemplateKey} " +
            "</script>")
    NewsTemplateBean getTemplate(@Param("newsTemplateKey") String newsTemplateKey);



    @ResultMap("NewsTemplateBean")
    @Select("<script>" +
            "select m.* from gs_news_template as m " +
            "where m.news_template_key in " +
            "   <foreach item=\"item\" collection=\"newsTemplateKeys\" open=\"(\" close=\")\" separator=\",\"> " +
            "       #{item}" +
            "   </foreach> " +
            "</script>")
    List<NewsTemplateBean> getTemplateBatch(@Param("newsTemplateKeys") List<String> newsTemplateKeys);


    @Select("<script>" +
            "delete from gs_news_template " +
            "where news_template_key in " +
            "   <foreach item=\"item\" collection=\"newsTemplateKeys\" open=\"(\" close=\")\" separator=\",\"> " +
            "       #{item}" +
            "   </foreach> " +
            "</script>")
    List<NewsTemplateBean> deleteBatch(@Param("newsTemplateKeys") List<String> newsTemplateKeys);



//    @Insert("<script>" +
//            "insert into gs_news_template(" +
//            "   news_template_key," +
//            "   edifice_key," +
//            "   platform," +
//            "   template_key," +
//            "   name," +
//            "   title," +
//            "   params," +
//            "   content," +
//            "   sms_content," +
//            "   sms_template_code," +
//            "   enable," +
//            "   create_time," +
//            "   update_time," +
//            "   version," +
//            "   gs_delete," +
//            ") values(" +
//            "   #{bean.newsTemplateKey}," +
//            "   #{bean.edificeKey}," +
//            "   #{bean.platform}," +
//            "   #{bean.templateKey}," +
//            "   #{bean.name}," +
//            "   #{bean.title}," +
//            "   #{bean.params, typeHandler=com.gonsin.gnews.annotations.model.NewsParamsPoConverter}," +
//            "   #{bean.content}," +
//            "   #{bean.smsContent}," +
//            "   #{bean.templateCode}," +
//            "   #{bean.enable}," +
//            "   now()," +
//            "   now()," +
//            "   1," +
//            "   0" +
//            ")" +
//            "</script>")

    @Insert("<script>" +
            "insert into gs_news_template set " +
            "   news_template_key = #{bean.newsTemplateKey}," +
            "   edifice_key = #{bean.edificeKey}," +
            "   platform = #{bean.platform}," +
            "   template_key = #{bean.templateKey}," +
            "   name = #{bean.name}," +
            "   title = #{bean.title}," +
            "   url = #{bean.url}," +
            "   params = #{bean.params, typeHandler=com.gonsin.gnews.annotations.model.NewsParamsPoConverter}," +
            "   content = #{bean.content}," +
            "   sms_content = #{bean.smsContent}," +
            "   sms_template_code = #{bean.templateCode}," +
            "   enable = #{bean.enable}," +
            "   default_content = #{bean.defaultContent}," +
            "   sort = #{bean.sort}," +
            "   init = #{bean.init}," +
            "   update_time = now()," +
            "   create_time = now()," +
            "   version = 1," +
            "   gs_delete = 0" +
            "</script>")
    void insert(@Param("bean") NewsTemplateBean bean);



    @Update("<script>" +
            "update gs_news_template set " +
            "   edifice_key = #{bean.edificeKey}," +
            "   platform = #{bean.platform}," +
            "   template_key = #{bean.templateKey}," +
            "   name = #{bean.name}," +
            "   title = #{bean.title}," +
            "   url = #{bean.url}," +
            "   params = #{bean.params, typeHandler=com.gonsin.gnews.annotations.model.NewsParamsPoConverter}," +
            "   content = #{bean.content}," +
            "   sms_content = #{bean.smsContent}," +
            "   sms_template_code = #{bean.templateCode}," +
            "   enable = #{bean.enable}," +
            "   sort = #{bean.sort}," +
            "   init = #{bean.init}," +
            "   update_time = now()," +
            "   version = version + 1," +
            "   gs_delete = 0 " +
            "where news_template_key = #{bean.newsTemplateKey}" +
            "</script>")
    void update(@Param("bean") NewsTemplateBean bean);


    /**
     * 查询模板数量
     */
    @Select("<script>" +
            "select ifnull(count(m.news_template_key), 0) from gs_news_template as m " +
            "where m.edifice_key = #{edificeKey} " +
            "<if test=\"searchPo.name != null and searchPo.name != ''\">" +
            "   and m.name like concat('%', #{searchPo.name}, '%') " +
            "</if>" +
            "<if test=\"searchPo.platform != null and searchPo.platform != ''\">" +
            "   and m.platform = #{searchPo.platform} " +
            "</if>" +
            "<if test=\"searchPo.templateKey != null and searchPo.templateKey != ''\">" +
            "   and m.template_key = #{searchPo.templateKey} " +
            "</if>" +
            "<if test=\"searchPo.enable != null\">" +
            "   and m.enable = #{searchPo.enable} " +
            "</if>" +
            "<if test=\"searchPo.newsTemplateKeys != null and searchPo.newsTemplateKeys.size() &gt; 0\">" +
            "   and m.news_template_key in " +
            "   <foreach item=\"item\" collection=\"searchPo.newsTemplateKeys\" open=\"(\" close=\")\" separator=\",\">" +
            "       #{item}" +
            "   </foreach>" +
            "</if>" +
            "<if test=\"searchPo.excludedPlatform != null and searchPo.excludedPlatform.size() &gt; 0\">" +
            "   and m.platform not in " +
            "   <foreach item=\"item\" collection=\"searchPo.excludedPlatform\" open=\"(\" close=\")\" separator=\",\">" +
            "       #{item}" +
            "   </foreach>" +
            "</if>" +
            "</script>")
    int countTemplates(
            @Param("edificeKey") String edificeKey,
            @Param("searchPo") NewsTemplateSearchPo searchPo);

    @Select("select ifNull(max(sort),0) from gs_news_template where edifice_key = #{edificeKey}")
    Integer getMaxSort(@Param("edificeKey") String edificeKey);

    @Select("select * from gs_news_template " +
            " where edifice_key = #{edificeKey} " +
            " and sort < #{sort} " +
            " order by sort desc ")
    List<NewsTemplateBean> getLtSortNewsTemplate(@Param("edificeKey") String edificeKey,
                                                 @Param("sort") Integer sort);

    @Select("select * from gs_news_template " +
            " where edifice_key = #{edificeKey} " +
            " and sort > #{sort} " +
            " order by sort asc ")
    List<NewsTemplateBean> getGtSortNewsTemplate(@Param("edificeKey") String edificeKey,
                                                 @Param("sort") Integer sort);
}
